﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using xTeam.HengLe.Entity;
using Valor.Helper;
using System.Web;

namespace xTeam.HengLe.Controller.Website {
    public class ProductController : BaseController {
        protected override void Initialize(System.Web.Routing.RequestContext requestContext) {
            base.Initialize(requestContext);
            this.SiteMapPathAdd(ViewsResources.SiteMapPath.Product, Url.Action("Index"));
        }


        /// <summary>
        /// 
        /// </summary>
        public ActionResult Index(int classId = 0, DateTime? archive = null) {
            return RedirectToAction("List", new { classId = classId, archive = archive });
        }

        /// <summary>
        /// 
        /// </summary>
        public ActionResult List(int pageIndex = 1, int classId = 0, string key = "", DateTime? archive = null) {
            int pageCount = 0;
            ProductViewModel viewModel = new ProductViewModel();
            EClassify classify = _classifyDataAccess.GetClassifyById(classId) ?? new EClassify() { Title = ViewsResources.Shared.AllProducts };
            viewModel.ImageRoot = _imageRoot;
            viewModel.ProductList = _productDataAccess.Search(16, pageIndex, classId, key, _user.Uid, archive, ref pageCount);
            viewModel.Classify = classify;
            ViewData["Pager"] = this.CreatePager(Url.Action("List"
                , new {
                    pageIndex = "{0}",
                    classId = string.IsNullOrEmpty(key) ? classId : 0,
                    key = key
                }), pageCount, pageIndex);
            // 站点路径
            List<EClassify> pathList = _classifyDataAccess.GetParentList(classId, "Product");
            foreach (var item in pathList) {
                this.SiteMapPathAdd(item.Title, Url.Action("Index", new { classid = item.ClassifyId }));
            }
            this.SiteMapPathAdd(ViewsResources.SiteMapPath.List);

            return View(viewModel);
        }


        public ActionResult ProductPart(string partName = "") {

            int pageCount = 0;
            ProductViewModel viewModel = new ProductViewModel();
            viewModel.IsAuth = this.User.Identity.IsAuthenticated;
            var productList = _productDataAccess.Search(12, 0, 0, string.Empty, _user.Uid, null, ref pageCount);
            viewModel.ProductList = productList;
            viewModel.ImageRoot = _imageRoot;
            return View(viewModel);
        }

        /// <summary>
        /// 
        /// </summary>
        public ActionResult Preview(int productId = 0) {
            EProduct product = _productDataAccess.GetBaseById(productId, _user.Uid);
            if (product == null)
                throw new HttpException(404, "Not Found !");
            ProductViewModel viewModel = new ProductViewModel() { ImageRoot = _imageRoot };
            viewModel.GuestBook = new GuestBookViewModel() {
                GuestBookInfo = new EGuestBook() { ProductId = productId },
                GuestBookList = _guestBookDataAccess.Search(productId)
            };
            viewModel.ProductView = product;
            viewModel.Details = _productDataAccess.GetDeatails(productId);
            viewModel.ImageList = _productDataAccess.GetImageList(productId);
            viewModel.OptionalList = _productDataAccess.ProductExtendList(productId);

            // 站点路径
            List<EClassify> pathList = _classifyDataAccess.GetParentList(product.ClassId, "Product");
            foreach (var item in pathList) {
                this.SiteMapPathAdd(item.Title, Url.Action("Index", new { classid = item.ClassifyId }));
            }
            this.SiteMapPathAdd(ViewsResources.SiteMapPath.ProductView);


            return View(viewModel);
        }

        public ActionResult Archive() {
            this.SiteMapPathAdd(ViewsResources.SiteMapPath.Archive);

            DateTime dt;
            DateTime dtStart = new DateTime(2011, 4, 1);
            DateTime dtEnd = DateTime.Now.Date;
            string url, s;

            List<string> archives = new List<string>();
            for (int year = dtStart.Year; year <= dtEnd.Year; year++) {
                for (int month = 1; month <= 12; month++) {
                    dt = new DateTime(year, month, 1);
                    if (dt.Date < dtStart.Date || dt.Date > dtEnd.Date) {
                        continue;
                    }
                    url = Url.Action("Index", new { archive = dt.Date.ToString("yyyy-MM-dd") });
                    s = string.Format("{0} {1}", dt.Date.ToString("yyyy-MM"), this._langName != "zh-cn" ? "上旬" : "EARLY");
                    archives.Add(string.Format("<a href='{0}'>{1}</a>", url, s));
                    dt = new DateTime(year, month, 15);
                    if (dt.Date < dtStart.Date || dt.Date > dtEnd.Date) {
                        continue;
                    }
                    url = Url.Action("Index", new { archive = dt.Date.ToString("yyyy-MM-dd") });
                    s = string.Format("{0} {1}", dt.Date.ToString("yyyy-MM"), this._langName != "zh-cn" ? "下旬" : "EARLY");
                    archives.Add(string.Format("<a href='{0}'>{1}</a>", url, s));
                }
            }
            return View(archives);
        }
    }
}
